package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import io.lkt.common.util.Query;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.*;
import io.lkt.modules.sys.service.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 素材点击日志
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-02-22 09:59:38
 */
@RestController
@RequestMapping("/sys/articlelookdetaillog")
public class ArticleLookDetailLogController {
    @Autowired
    private BArticleTypeInfoService bArticleTypeInfoService;
    @Autowired
    private BArticleInfoService bArticleInfoService;
    @Autowired
    private ArticleLookDetailLogDBService articleLookDetailLogDbService;
    @Autowired
    BBrandInfoService bBrandInfoService;
    @Autowired
    BUserInfoService bUserInfoService;

    @RequestMapping("/lookListForUserId")
    public R lookListForUserId(@RequestParam Map<String, Object> params) {
        PageUtils page = articleLookDetailLogDbService.queryPageForUserId(params);

        List<?> list = page.getList();

        if (CollectionUtils.isNotEmpty(list)) {
            List<BArticleInfoEntity> bArticleInfoList = Lists.newArrayList();

            for (Object temp : list) {
                ArticleLookDetailLogDBEntity info = (ArticleLookDetailLogDBEntity) temp;

                BArticleInfoEntity bArticleInfo = new BArticleInfoEntity();
                bArticleInfo.setId(info.getArticleId());

                bArticleInfo = bArticleInfoService.getById(info.getArticleId());
                bArticleInfo.setShareTime(info.getLookTime());
                // 分类
                if ((bArticleInfo.getArticleTypeId() != null) && (!bArticleInfo.getArticleTypeId().equals(""))) {
                    BArticleTypeInfoEntity bArticleTypeInfoEntity = this.bArticleTypeInfoService.getById(bArticleInfo.getArticleTypeId());
                    if (bArticleTypeInfoEntity != null) {
                        bArticleInfo.setArticleTypeName(bArticleTypeInfoEntity.getArticleTypeName());
                    }
                }

                // 品牌
                if ((bArticleInfo.getArticleBrandId() != null) && (!bArticleInfo.getArticleBrandId().equals(""))) {
                    BBrandInfoEntity bBrandInfoEntity = this.bBrandInfoService.getById(bArticleInfo.getArticleBrandId());
                    if (bBrandInfoEntity != null) {
                        bArticleInfo.setArticleTypeName(bBrandInfoEntity.getBrandName());
                    }
                }

                bArticleInfoList.add(bArticleInfo);
            }

            page.setList(bArticleInfoList);
        }

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:articlelookdetaillog:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = articleLookDetailLogDbService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:articlelookdetaillog:info")
    public R info(@PathVariable("id") Integer id){
        ArticleLookDetailLogDBEntity articleLookDetailLog = articleLookDetailLogDbService.getById(id);

        return R.ok().put("articleLookDetailLog", articleLookDetailLog);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:articlelookdetaillog:save")
    public R save(@RequestBody ArticleLookDetailLogDBEntity articleLookDetailLog){
        articleLookDetailLogDbService.save(articleLookDetailLog);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:articlelookdetaillog:update")
    public R update(@RequestBody ArticleLookDetailLogDBEntity articleLookDetailLog){
        ValidatorUtils.validateEntity(articleLookDetailLog);
        articleLookDetailLogDbService.updateById(articleLookDetailLog);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:articlelookdetaillog:delete")
    public R delete(@RequestBody Integer[] ids){
        articleLookDetailLogDbService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/userLookInfo")
    public R getUserLookInfo(@RequestParam Map<String, Object> params) {
        if (params.get("id") == null) return R.error("访问失败");
        if (params.get("resourceType") == null || params.get("resourceType").equals("")) {
            params.put("resourceType", 1); // 默认访问文章类型
        }
        Query query = new Query(params);
        List<ArticleLookDetailLogDBEntity> list = articleLookDetailLogDbService.getUserLookInfo(query);
        int total = articleLookDetailLogDbService.getUserLookInfoTotal(params);
        PageUtils page = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", page);
    }

    @RequestMapping("/getUserLookCountByArticleId")
    public R getUserLookCountByArticleId(@RequestParam Map<String, Object> params) {
        if (params.get("id") == null) return R.error("访问失败");
        if (params.get("resourceType") == null || params.get("resourceType").equals("")) {
            params.put("resourceType", 1); // 默认访问文章类型
        }
        int userLookCount = articleLookDetailLogDbService.getUserLookInfoTotal(params);
        return R.ok().put("userLookCount", userLookCount);
    }

    @RequestMapping("/getUserLookCount/{userId}")
    public R getUserLookCount(@PathVariable("userId") Integer userId) {
        if (userId == null || userId <= 0)
            return R.error("用户不存在");
        BUserInfoEntity bUserInfoEntity = bUserInfoService.queryUserInfo(userId);

        int userLookCount = articleLookDetailLogDbService.getUserLookCount(userId, bUserInfoEntity.getGzhCode());
        return R.ok().put("userLookCount", userLookCount);
    }

}

